package com.cly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cly.domain.Order;
import com.cly.service.OrderService;
import com.cly.vo.OrderConfirm;
import com.cly.vo.OrderResult;
import com.cly.vo.OrderStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("p/myOrder")
@Api("订单接口管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("orderCount")
    @ApiOperation("获取用户的订单状态")
    public ResponseEntity<OrderStatus> getOrderCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderStatus orderStatus = orderService.getOrderCount(userId);
        return ResponseEntity.ok(orderStatus);
    }

    @GetMapping("myOrder")
    @ApiOperation("分页查询用户订单详情")
    public ResponseEntity<Page<Order>> getOrderPage(Page<Order> page, Integer status){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Page<Order> orderPage = orderService.getOrderPage(page,status,userId);
        return ResponseEntity.ok(orderPage);
    }

    @PostMapping("confirm")
    @ApiOperation("订单确认接口")
    public ResponseEntity<OrderResult> confirmOrder(@RequestBody OrderConfirm orderConfirm){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderResult orderResult = orderService.orderConfirm(userId,orderConfirm);
        return ResponseEntity.ok(orderResult);
    }

    @PostMapping("submit")
    @ApiOperation("订单提交")
    public ResponseEntity<String> submit(@RequestBody OrderResult orderResult){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String orderNum = orderService.submit(userId,orderResult);
        return ResponseEntity.ok(orderNum);
    }

    /**
     * 根据订单号 获取订单对象
     * @param orderNum
     * @return
     */
    @GetMapping("getOrderByNum")
    Order getOrderByNum(@RequestBody String orderNum) {
        return orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderNum)
        );
    }

    /**
     * 根据订单号查询订单支付状态
     * @param orderSn
     * @return
     */
    @GetMapping("query")
    public ResponseEntity<Boolean> queryOrderStatus(@RequestParam String orderSn){
        int count = orderService.count(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderSn)
                .eq(Order::getStatus, 2)
        );
        return ResponseEntity.ok(count > 0);
    }

    /**
     * 修改订单状态 已经支付
     * @param orderNum
     */
    @PostMapping("changeOrderStatus")
    void changeOrderStatus(@RequestBody String orderNum){
        orderService.changeOrderStatus(orderNum);
    }

}
